package ods_table

import (
	"fmt"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"

	"server/apps/ods/ods_field"
	"server/apps/ods/ods_job"
	"server/global"
	"server/model/common/request"
	"server/model/common/response"
)

type OdsDbTableApi struct {
}

var odsDbTableService = OdsDbTableService{}


func (odsDbTableApi *OdsDbTableApi) CreateOdsDbTable(c *gin.Context) {
	var odsDbTable OdsDbTable
	err := c.ShouldBindJSON(&odsDbTable)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err = odsDbTable.SetInit(); err != nil {
		msg := fmt.Sprintf("更新失败:%s", err)
		response.FailWithMessage(msg, c)
		return
	}

	if err := odsDbTableService.CreateOdsDbTable(&odsDbTable); err != nil {
		global.Logger.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}


func (odsDbTableApi *OdsDbTableApi) DeleteOdsDbTable(c *gin.Context) {
	var odsDbTable OdsDbTable
	err := c.ShouldBindJSON(&odsDbTable)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := odsDbTableService.DeleteOdsDbTable(odsDbTable); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}


func (odsDbTableApi *OdsDbTableApi) DeleteOdsDbTableByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := odsDbTableService.DeleteOdsDbTableByIds(IDS); err != nil {
		global.Logger.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}


func (odsDbTableApi *OdsDbTableApi) UpdateOdsDbTable(c *gin.Context) {
	var odsDbTable OdsDbTable
	var err error
	err = c.ShouldBindJSON(&odsDbTable)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err = odsDbTable.SetInit(); err != nil {
		msg := fmt.Sprintf("更新失败:%s", err)
		response.FailWithMessage(msg, c)
		return
	}
	if err := odsDbTableService.UpdateOdsDbTable(odsDbTable); err != nil {
		global.Logger.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}


func (odsDbTableApi *OdsDbTableApi) FindOdsDbTable(c *gin.Context) {
	var odsDbTable OdsDbTable
	err := c.ShouldBindQuery(&odsDbTable)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reodsDbTable, err := odsDbTableService.GetOdsDbTableById(odsDbTable.ID); err != nil {
		global.Logger.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reodsDbTable": reodsDbTable}, c)
	}
}


func (odsDbTableApi *OdsDbTableApi) GetOdsDbTableList(c *gin.Context) {
	var pageInfo OdsDbTableSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := odsDbTableService.GetOdsDbTableInfoList(pageInfo); err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}


func (odsDbTableApi *OdsDbTableApi) GetAllOdsDbTables(c *gin.Context) {
	odsDbTable, err := odsDbTableService.GetAllOdsDbTables()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"odsDbTable": odsDbTable}, "获取成功", c)
	}
}


func (odsDbTableApi *OdsDbTableApi) DelOdsTbFld(c *gin.Context) {
	var odsDbTable OdsDbTable
	err0 := c.ShouldBindJSON(&odsDbTable)
	if err0 != nil {
		response.FailWithMessage(err0.Error(), c)
		return
	}

	odsTbName := odsDbTable.OdsTbName

	sqlDropTable := fmt.Sprintf("DROP TABLE IF EXISTS %s;", odsTbName)
	_, err := global.OdsDB.Exec(sqlDropTable)
	if err != nil {
		global.Logger.Info(sqlDropTable)
		msg := fmt.Sprintf("删除ODS:%s表失败", sqlDropTable)
		response.FailWithMessage(msg, c)
		return
	}

	tx1 := global.SysDB.Unscoped().Delete(OdsDbTable{}, "odsTbName = ?", odsTbName)
	if tx1.Error != nil {
		err1Msg := fmt.Sprintf("OdsDbTable删除表名失败:%s", tx1.Error)
		global.Logger.Info(err1Msg)
		response.FailWithMessage(err1Msg, c)
		return
	}

	tx2 := global.SysDB.Unscoped().Delete(ods_field.OdsDbField{}, "odsTbName = ?", odsTbName)
	if tx2.Error != nil {
		errMsg := fmt.Sprintf("OdsDbField删除表%s字段失败:%s", odsTbName, tx2.Error)
		global.Logger.Info(errMsg)
		response.FailWithMessage(errMsg, c)
		return
	}

	tx4 := global.SysDB.Unscoped().Delete(ods_job.OdsJob{}, "jobCode = ?", odsTbName)
	if tx4.Error != nil {
		errMsg := fmt.Sprintf("OdsJob删除表%s字段失败:%s", odsTbName, tx4.Error)
		global.Logger.Info(errMsg)
		response.FailWithMessage(errMsg, c)
		return
	}
	response.OkWithMessage(fmt.Sprintf("删除%s成功", odsTbName), c)
}
